// PaperController.java
package com.example.demo.controller;

import com.example.demo.entity.Paper;
import com.example.demo.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/api/papers")
@CrossOrigin
public class PaperController {
    @Autowired
    private PaperService paperService;

    // 获取所有论文
    @GetMapping
    public List<Paper> getAllPapers() {
        return paperService.findAllPapers();
    }

    // 创建新论文
    @PostMapping
    public Paper createPaper(@RequestBody Paper paper) {
        return paperService.createPaper(paper);
    }

    // 更新论文
    @PutMapping("/{id}")
    public Paper updatePaper(@PathVariable Long id, @RequestBody Paper paper) {
        // 假设paper对象已经包含了更新后的数据
        return paperService.updatePaper(id, paper);
    }

    // 删除论文
    @DeleteMapping("/{id}")
    public void deletePaper(@PathVariable Long id) {
        paperService.deletePaper(id);
    }

    // 按ID查询单篇论文
    @GetMapping("/{id}")
    public Paper getPaperById(@PathVariable Long id) {
        return paperService.findPaperById(id);
    }
}